<template>
  <div>
     <!-- 面包屑导航 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">商品</el-breadcrumb-item>
      <el-breadcrumb-item><a href="/">商品审核</a></el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>
    <el-card shadow="hover">
      <div slot="header" class="clearfix">
        <span><i icon="el-icon-search"></i>筛选查询</span>
      </div>
      <div class="text item">
        <el-form :inline="true" :model="searchInfo" class="demo-form-inline" size="small">
          <el-form-item label="输入商品">
            <el-input v-model="searchInfo.name" placeholder="商品名称" @input="e => searchInfo.name = validForbid (e)"></el-input>
          </el-form-item>
          <el-form-item label="商品分类">
          <!-- 这里对category表进行数据回显，显示问二级分类 -->
           <el-cascader
            v-model="category3Ids"
            :options="options" clearable></el-cascader>
        </el-form-item>
        <el-form-item label="商品品牌">
          <el-select v-model="searchInfo.brandId" placeholder="商品品牌" clearable>
             <el-option
                  v-for="item in brandData" :key="item.id" :label="item.name" :value="item.id"
                  ></el-option>
          </el-select>
        </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="searchPage">查询</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
        <el-card shadow="hover">
           <el-table
              :data="tableData"
              style="width: 100%">
              <el-table-column
                prop="id"
                label="编号"
                width="180">
              </el-table-column>
              <el-table-column
                prop="image"
                label="商品图片"
                style="width: 100px; height: 50px">
                <template slot-scope="scope">
                  <el-image :src="scope.row.image"/>
                </template>
              </el-table-column>
              <el-table-column
                prop="name"
                label="商品名称">
              </el-table-column>
              <el-table-column
                prop="sn"
                label="货号">
              </el-table-column>
              <el-table-column
                label="标签">
                <template slot-scope="scope">
                   <el-tooltip class="item" effect="dark" content="审核通过自动上架" placement="bottom">
                      <el-switch
                        v-model="scope.row.isMarketable"
                        active-color="#13ce66"
                        inactive-color="#ff4949"
                        active-value="1"
                        inactive-value="0" disabled>
                      </el-switch>
                   </el-tooltip>
                </template>
              </el-table-column>
              <el-table-column
                prop="seq"
                label="排序">
              </el-table-column>
              <el-table-column
                prop="saleNum"
                label="销量">
              </el-table-column>
              <el-table-column
                label="审核状态">
                <template slot-scope="scope">
                  {{scope.row.status === "0" ? "未审核" : "已审核"}}
                </template>
              </el-table-column>
              <el-table-column
                label="操作" width="150px">
                <template slot-scope="scope">
                  <el-button size="mini" plain type="success" @click="dialogVisible = true">查看</el-button>
                  <el-button  size="mini" plain type="success"  @click="auditDialogVisible = true; formLabelAlign.id = scope.row.id;
                  formLabelAlign.name = scope.row.name">审核</el-button>
                </template>
              </el-table-column>
            </el-table>
             <!-- 分页插件 -->
            <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="pageNum"
              :page-sizes="[8, 16, 32, 64]"
              :page-size="pageSize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total">
            </el-pagination>
        </el-card>
    </el-card>
    <!-- 审核信息 -->
    <el-dialog
      title="商品审核"
      :visible.sync="auditDialogVisible"
      width="30%"
      :before-close="auditHandleClose">
        <el-card>
          <el-form label-position="left" label-width="80px" :model="formLabelAlign">
            <el-form-item label="商品名称">
              <el-input v-model="formLabelAlign.name" readonly></el-input>
            </el-form-item>
            <el-form-item label="商品审核">
               <el-radio-group v-model="formLabelAlign.status">
                <el-radio :label="1">通过</el-radio>
                <el-radio :label="0">不通过</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="反馈详情">
              <el-input type="textarea" v-model="formLabelAlign.message" @input="e => formLabelAlign.message = validForbid (e)"></el-input>
            </el-form-item>
          </el-form>
        </el-card>
      <span slot="footer" class="dialog-footer">
        <el-button @click="auditHandleClose">取 消</el-button>
        <el-button type="primary" @click="audit">确 定</el-button>
      </span>
    </el-dialog>
    <!-- TODO 待完善  -->
    <el-dialog
      title="详情信息"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose">
        <el-card>
          <el-table
            :data="auditData"
            height="250"
            border
            style="width: 100%">
            <el-table-column
              prop="date"
              label="审核时间"
              width="180">
            </el-table-column>
            <el-table-column
              prop="name"
              label="审核人员"
              width="180">
            </el-table-column>
            <el-table-column
              prop="address"
              label="审核结果">
            </el-table-column>
            <el-table-column
              prop="address"
              label="反馈信息">
            </el-table-column>
          </el-table>
        </el-card>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      auditData: [],
      dialogVisible: false,
      category3Ids: [],
      searchInfo: {},
      options: [],
      brandData: [],
      tableData: [],
      pageNum: 0,
      pageSize: 8,
      total: 0,
      auditDialogVisible: false,
      formLabelAlign: {} // 审核数据
    }
  },
  created () {
    this.searchPage()
    this.menthodBrandData()
  },
  methods: {
    auditHandleClose () {
      this.formLabelAlign = {}
      this.auditDialogVisible = false
    },
    handleClose () {
      this.dialogVisible = false
    },
    async audit () {
      console.log(this.formLabelAlign)
      const { data: res } = await this.$http.put(`/goods/spu/audit`, this.formLabelAlign)
      if (res.code === 200) this.$message.success('修改成功')
      this.auditHandleClose()
      this.searchPage()
    },
    selectRow () {

    },
    handleSizeChange (newSize) {
      this.pageSize = newSize
      this.searchPage()
    },
    handleCurrentChange (newNum) {
      this.pageNum = newNum
      this.searchPage()
    },
    async searchPage () {
      this.searchInfo.category3Id = ''
      if (this.category3Ids.length > 0) { this.searchInfo.category3Id = this.category3Ids[this.category3Ids.length - 1] }
      const { data: res } = await this.$http.post(`/goods/spu/queryAudit/${this.pageNum}/${this.pageSize}`, this.searchInfo)
      this.tableData = res.data.pageUtils.rows
      this.total = res.data.pageUtils.total
    },
    // 回显商品分类的联动信息
    // 回显商品品牌
    async menthodBrandData () {
      const { data: res } = await this.$http.get('/goods/brand/list')
      this.brandData = res.data.list
      const { data: res2 } = await this.$http.get('/goods/category/list')
      this.options = res2.data.list
    }
  }
}
</script>

<style scoped>
.text {
    font-size: 14px;
  }

  .item {
    margin-bottom: 18px;
  }

  .clearfix:before,
  .clearfix:after {
    display: table;
    content: "";
  }
  .clearfix:after {
    clear: both
  }

</style>
